from sqlalchemy.orm import Session

from crud import AttendanceAdminCrud, StudentAdminCrud
from model.TeachingModel import AttendanceModel
from schema.QuerySchema import QueryBase, AttendanceQuery, AttendanceDateIntervalQuery
from schema.TeachingSchema import StudentInfo, BatchAttendanceInfo, AttendanceInfo


class AttendanceAdminController:
    @staticmethod
    def mod_attendance(attendanceInfo: AttendanceInfo, db: Session):
        return AttendanceAdminCrud.mod_attendance(attendanceInfo, db)

    @staticmethod
    def query_attendance_by_field(attendanceQuery: AttendanceQuery, db: Session):
        return AttendanceAdminCrud.query_attendance_by_field(attendanceQuery, db)

    @staticmethod
    def query_attendance_by_dateInterval(attendanceDateIntervalQuery: AttendanceDateIntervalQuery, db: Session):
        return AttendanceAdminCrud.query_attendance_by_dateInterval(attendanceDateIntervalQuery, db)
    @staticmethod
    def query_attendance_by_class_and_date(attend_date: str, class_id: int, db: Session):
        return AttendanceAdminCrud.query_attendance_by_class_and_date(attend_date, class_id, db)

    @staticmethod
    def query_attendance_by_page(query_base: QueryBase, db: Session):
        return AttendanceAdminCrud.query_attendance_by_page(query_base, db)

    @staticmethod
    def batch_add_attendance(batchAttendanceInfo: BatchAttendanceInfo, db: Session):
        attendance_list = []
        response = StudentAdminCrud.query_student_by_class_id(batchAttendanceInfo.class_id, db)

        if response.get('code') == 0:
            data = response.get('data')
            for student in data:
                attendanceModel = AttendanceModel(attend_date=batchAttendanceInfo.attend_date, class_id = batchAttendanceInfo.class_id, abnormal_type=1,
                                                  is_normal=True)
                attendanceModel.student_id = student.id

                attendance_list.append(attendanceModel)
        else:
            return response

        return AttendanceAdminCrud.batch_add_attendance(attendance_list, db)
